package com.boot.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.boot.constant.Constants;
import com.boot.communal.response.ResponseServer;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝支付封装
 * @author zhouJie
 */
public class AliPayUtil {

    /**
     * 支付宝支付 - 拼团
     * @param map
     * @return
     */
    public static ResponseServer aliPaymentCollage(Map<String,Object> map){
        Map<String, Object> maps = new HashMap<>();
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = map.get("outTradeNo").toString();
        // 订单名称，必填
        String subject =map.get("body").toString();
        // 付款金额，必填
        String total_amount = map.get("totalFee").toString();
        // 商品描述，可空
        String body = "";
        // 超时时间 可空
        String timeout_express = "30m";
        // 销售产品码 必填
        String product_code = "QUICK_MSECURITY_PAY";
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        // 调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(Constants.aliPay_gatewayUrl, Constants.aliPay_app_id,
                Constants.merchant_private_key, Constants.aliPay_format, Constants.aliPay_charset,
                Constants.aliPay_public_key, Constants.aliPay_sign_type);

        AlipayTradeAppPayRequest aliPay_request = new AlipayTradeAppPayRequest();
        // 封装请求支付信息
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(body);
        model.setSubject(subject);
        model.setOutTradeNo(out_trade_no);
        model.setTimeoutExpress(timeout_express);
        model.setTotalAmount(total_amount);
        model.setProductCode(product_code);
        aliPay_request.setBizModel(model);
        // 设置异步通知地址
        aliPay_request.setNotifyUrl(Constants.aliPay_notify_url_collage);
        // 设置同步地址
        aliPay_request.setReturnUrl(Constants.aliPay_notify_url_collage);
        // form表单生产
        String form;
        try {
            // 调用SDK生成表单
            form = client.sdkExecute(aliPay_request).getBody();
            maps.put("result", form);
            return ResponseServer.success("成功", form);
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ResponseServer.error(e.getMessage());
        }
    }

    /**
     * 支付宝支付 - 共享
     * @param map
     * @return
     */
    public static ResponseServer aliPaymentShare(Map<String,Object> map){
        Map<String, Object> maps = new HashMap<>();
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = map.get("outTradeNo").toString();
        // 订单名称，必填
        String subject = map.get("body").toString();
        // 付款金额，必填
        String total_amount = map.get("totalFee").toString();
        // 商品描述，可空
        String body = "";
        // 超时时间 可空
        String timeout_express = "30m";
        // 销售产品码 必填
        String product_code = "QUICK_MSECURITY_PAY";
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        // 调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(Constants.aliPay_gatewayUrl, Constants.aliPay_app_id,
                Constants.merchant_private_key, Constants.aliPay_format, Constants.aliPay_charset,
                Constants.aliPay_public_key, Constants.aliPay_sign_type);
        AlipayTradeAppPayRequest aliPay_request = new AlipayTradeAppPayRequest();
        // 封装请求支付信息
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(body);
        model.setSubject(subject);
        model.setOutTradeNo(out_trade_no);
        model.setTimeoutExpress(timeout_express);
        model.setTotalAmount(total_amount);
        model.setProductCode(product_code);
        aliPay_request.setBizModel(model);
        // 设置异步通知地址
        aliPay_request.setNotifyUrl(Constants.aliPay_notify_url_share);
        // 设置同步地址
        aliPay_request.setReturnUrl(Constants.aliPay_notify_url_share);
        // form表单生产
        String form;
        try {
            // 调用SDK生成表单
            form = client.sdkExecute(aliPay_request).getBody();
            maps.put("result", form);
            return ResponseServer.success("成功", form);
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ResponseServer.error(e.getMessage());
        }
    }

}
